Extracting Features from Image

feature extraction.png

In [ ]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from skimage.io import imread, imshow

image = imread('/8_img.png', as_gray=True)
imshow(image)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ac76b1a6f20>

Method #1: Grayscale Pixel Values as Features

In [ ]:
#checking image shape
image.shape, image
Out[ ]:
((300, 236),
 array([[1.        , 1.        , 0.99607843, ..., 1.        , 1.        ,
         1.        ],
        [1.        , 1.        , 0.99607843, ..., 1.        , 0.99607843,
         1.        ],
        [1.        , 1.        , 1.        , ..., 1.        , 1.        ,
         0.99607843],
        ...,
        [1.        , 0.99607843, 0.99607843, ..., 1.        , 0.99607843,
         0.99607843],
        [1.        , 1.        , 0.99607843, ..., 1.        , 1.        ,
         1.        ],
        [1.        , 1.        , 1.        , ..., 1.        , 1.        ,
         1.        ]]))

Original Image

In [ ]:
image = imread('/puppy.jpeg')
image.shape, imshow(image)
Out[ ]:
((168, 300, 3), <matplotlib.image.AxesImage at 0x7ac76b102800>)

Converting into grascale image

In [ ]:
image = imread('/puppy.jpeg', as_gray=True)
image.shape, imshow(image)
Out[ ]:
((168, 300), <matplotlib.image.AxesImage at 0x7ac76b06f820>)
In [ ]:
image = imread('/banyan leaf.jpeg')
image.shape, imshow(image)
Out[ ]:
((275, 183, 3), <matplotlib.image.AxesImage at 0x7ac76b0df580>)
In [ ]:
image = imread('/banyan leaf.jpeg', as_gray=True)
image.shape, imshow(image)
Out[ ]:
((275, 183), <matplotlib.image.AxesImage at 0x7ac76966f250>)

Read the Image Data

In [ ]:
from skimage.io import imread, imshow
image = imread('/banyan leaf.jpeg', as_gray=True)
#checking image shape
print('Shape of the image is = ',image.shape)
#checking image shape
image.shape, image
# image matrix
print('\n\nImage matrix\n\n',image)
Shape of the image is =  (275, 183)


Image matrix

 [[0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]
 [0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]
 [0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]
 ...
 [0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]
 [0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]
 [0.91764706 0.91764706 0.91764706 ... 0.91764706 0.91764706 0.91764706]]

Shape of the image

In [ ]:
image = imread('/banana leaf.jpeg')
image.shape, imshow(image)
print('\n\nShape of the image = ',image.shape)
print('\n\nSize of the image = ',image.size)
print('\n\nDatatype of the image = ',image.dtype)

Shape of the image =  (183, 275, 3)


Size of the image =  150975


Datatype of the image =  uint8
In [ ]:
image = imread('/banana leaf.jpeg', as_gray=True)
image.shape, imshow(image)
print('\n\nShape of the image = ',image.shape)
print('\n\nSize of the image = ',image.size)
print('\n\nDatatype of the image = ',image.dtype)

Shape of the image =  (183, 275)


Size of the image =  50325


Datatype of the image =  float64
In [ ]:
image = imread('/tomato-plant.jpg')
image.shape, imshow(image)
Out[ ]:
((1046, 1569, 3), <matplotlib.image.AxesImage at 0x7ac761728f10>)
In [ ]:
image = imread('/tomato-plant.jpg', as_gray=True)
print(image.shape)
imshow(image)
(1046, 1569)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ac76149e500>

Method #3: Extracting Edge Features

In [ ]:
#importing the required libraries
import numpy as np
from skimage.io import imread, imshow
from skimage.filters import prewitt_h,prewitt_v
import matplotlib.pyplot as plt
%matplotlib inline

#reading the image
image = imread('/banana leaf.jpeg',as_gray=True)

#calculating horizontal edges using prewitt kernel
edges_prewitt_horizontal = prewitt_h(image)
#calculating vertical edges using prewitt kernel
edges_prewitt_vertical = prewitt_v(image)

imshow(edges_prewitt_vertical, cmap='gray')
Out[ ]:
<matplotlib.image.AxesImage at 0x7ac760f2f2b0>
In [ ]:
#reading the image
image = imread('/tajmahal.jpeg')
print(image.shape)
imshow(image)
(183, 275, 3)
Out[ ]:
<matplotlib.image.AxesImage at 0x7ac760ce70d0>
In [ ]:
#importing the required libraries
import numpy as np
from skimage.io import imread, imshow
from skimage.filters import prewitt_h,prewitt_v
import matplotlib.pyplot as plt
%matplotlib inline

#reading the image
image = imread('/tajmahal.jpeg',as_gray=True)

#calculating horizontal edges using prewitt kernel
edges_prewitt_horizontal = prewitt_h(image)
#calculating vertical edges using prewitt kernel
edges_prewitt_vertical = prewitt_v(image)

imshow(edges_prewitt_vertical, cmap='gray')
Out[ ]:
<matplotlib.image.AxesImage at 0x7ac760de31c0>

Compute the mean pixel value of each channel (Red, Green, Blue) for a given image

In [ ]:
image = imread('/maize.jpeg')
image.shape, imshow(image)
Out[ ]:
((225, 225, 3), <matplotlib.image.AxesImage at 0x7ac751acc3a0>)
In [ ]:
import cv2
import numpy as np

# Load the image
image = cv2.imread('/maize.jpeg')

# Check if image is loaded properly
if image is None:
    print("Error: Unable to load image.")
else:
    # Split the image into its respective channels
    blue_channel, green_channel, red_channel = cv2.split(image)

    # Calculate the mean of each channel
    mean_blue = np.mean(blue_channel)
    mean_green = np.mean(green_channel)
    mean_red = np.mean(red_channel)

    print(f"Mean Blue: {mean_blue}")
    print(f"Mean Green: {mean_green}")
    print(f"Mean Red: {mean_red}")
Mean Blue: 175.28003950617284
Mean Green: 223.21827160493828
Mean Red: 225.21244444444446

Install OpenCV

In [ ]:
pip install opencv-python
Requirement already satisfied: opencv-python in /usr/local/lib/python3.10/dist-packages (4.8.0.76)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python) (1.25.2)

Load and Display the Image

In [ ]:
import cv2
import matplotlib.pyplot as plt

# Load the image
image = cv2.imread('/maize.jpeg', cv2.IMREAD_COLOR)

# Convert the image from BGR to RGB format for displaying using matplotlib
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the original image
plt.imshow(image_rgb)
plt.title('Original Image')
plt.axis('off')
plt.show()
In [ ]:
import cv2
import matplotlib.pyplot as plt

# Load the image
image = cv2.imread('/maize.jpeg', cv2.IMREAD_COLOR)

# Convert the image from BGR to RGB format for displaying using matplotlib
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Display the original image
plt.imshow(image_rgb)
plt.title('Maize / Corn')
plt.axis('off')
plt.show()

Convert the Image to Grayscale

In [ ]:
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
plt.imshow(gray_image, cmap='gray')
plt.title('Grayscale Image')
plt.axis('off')
plt.show()

Applying Edge Detection methods

In [ ]:
# Apply Canny edge detection
edges = cv2.Canny(gray_image, threshold1=100, threshold2=200)

# Display the edges
plt.imshow(edges, cmap='gray')
plt.title('Edge Image (Canny)')
plt.axis('off')
plt.show()
In [ ]:
# Apply Sobel edge detection
sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=5)

# Convert gradients to uint8
sobel_x = cv2.convertScaleAbs(sobel_x)
sobel_y = cv2.convertScaleAbs(sobel_y)

# Combine the Sobel X and Y results
sobel_combined = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)

# Display the Sobel edge detection results
plt.figure(figsize=(10, 5))

plt.subplot(1, 3, 1)
plt.imshow(sobel_x, cmap='gray')
plt.title('Sobel X')
plt.axis('off')

plt.subplot(1, 3, 2)
plt.imshow(sobel_y, cmap='gray')
plt.title('Sobel Y')
plt.axis('off')

plt.subplot(1, 3, 3)
plt.imshow(sobel_combined, cmap='gray')
plt.title('Sobel Combined')
plt.axis('off')

plt.show()
In [ ]:
# Save the edge images
cv2.imwrite('edges_canny.jpg', edges)
cv2.imwrite('sobel_x.jpg', sobel_x)
cv2.imwrite('sobel_y.jpg', sobel_y)
cv2.imwrite('sobel_combined.jpg', sobel_combined)
Out[ ]:
True

, Performing basic manipulations, and saving using opencv

In [ ]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Function to display images using matplotlib
def display_image(title, image):
    if len(image.shape) == 3:  # Color image
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        plt.imshow(image_rgb)
    else:  # Grayscale image
        plt.imshow(image, cmap='gray')
    plt.title(title)
    plt.axis('off')
    plt.show()

# Load a colored image from file
image = cv2.imread('/content/pomegran.jpeg')

# Check if the image was loaded successfully
if image is None:
    print("Error: Unable to load image.")
else:
    # Display the original image
    display_image('original Image', image)

    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    display_image('Grayscale Image', gray_image)

    # Resize the image to half its original size
    resized_image = cv2.resize(image, (image.shape[1] // 2, image.shape[0] // 2))
    display_image('Resized Image', resized_image)

    # Apply a Gaussian blur to the image
    blurred_image = cv2.GaussianBlur(image, (15, 15), 0)
    display_image('Blurred Image', blurred_image)

    # Save the images
    cv2.imwrite('output_image.jpg', image)
    cv2.imwrite('output_image_gray.jpg', gray_image)
    cv2.imwrite('output_image_resized.jpg', resized_image)
    cv2.imwrite('output_image_blurred.jpg', blurred_image)

    print("Images have been saved successfully.")
Images have been saved successfully.

Applying K-means clustering

In [ ]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Function to display images using matplotlib
def display_image(title, image):
    plt.imshow(image, cmap='gray')
    plt.title(title)
    plt.axis('off')
    plt.show()

# Load a colored image from file
image = cv2.imread('/content/rose.jpeg')

# Check if the image was loaded successfully
if image is None:
    print("Error: Unable to load image.")
else:
    # Convert the image to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Display the grayscale image
    display_image('Grayscale Image', gray_image)

    # Extract the grayscale pixel values as features
    features = gray_image.flatten()

    # Display some of the features
    print("First 100 pixel values as features:")
    print(features[:100])

    # Example: Use these features for a simple machine learning task (e.g., K-means clustering)
    from sklearn.cluster import KMeans

    # Reshape features to 2D array for clustering (necessary for some ML algorithms)
    reshaped_features = features.reshape(-1, 1)

    # Apply K-means clustering
    kmeans = KMeans(n_clusters=2, random_state=42)
    kmeans.fit(reshaped_features)

    # Get the clustered image by reshaping the labels back to the original image shape
    clustered_image = kmeans.labels_.reshape(gray_image.shape)

    # Display the clustered image
    display_image('Clustered Image', clustered_image)

    # Save the features to a file if needed
    np.save('grayscale_features.npy', features)
    print("Features saved to 'grayscale_features.npy'.")
First 100 pixel values as features:
[30 36 44 52 56 55 52 49 58 56 53 51 51 52 54 56 54 54 55 55 55 56 56 56
 49 49 48 46 45 44 43 43 56 44 30 21 21 25 27 28 12 16 21 24 24 23 23 24
 38 30 24 24 19 12 17 27 15 16 16 17 18 19 20 20 17 17 18 19 20 21 22 22
 10 17 24 26 23 19 19 21 30 29 29 28 28 27 27 26 33 29 26 27 32 36 35 32
 10 14 22 31]
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning
  warnings.warn(
Features saved to 'grayscale_features.npy'.

Flattenning matrix into a 1D array

In [ ]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Function to display images using matplotlib
def display_image(title, image, cmap='viridis'):
    plt.imshow(image, cmap=cmap)
    plt.title(title)
    plt.axis('off')
    plt.show()

# Load the image from file
image = cv2.imread('puppy.jpeg')

# Check if the image was loaded successfully
if image is None:
    print("Error: Unable to load image.")
else:
    # Display the original image
    display_image('Original Image', cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    # Initialize the feature matrix to store the mean pixel values
    feature_matrix = np.zeros((image.shape[0], image.shape[1]))

    # Calculate the mean pixel value for each pixel across the three channels
    for i in range(image.shape[0]):
        for j in range(image.shape[1]):
            feature_matrix[i][j] = np.mean(image[i, j, :])

    # Display the feature matrix (mean pixel values)
    display_image('Mean Pixel Value Image', feature_matrix, cmap='gray')

    # Flatten the feature matrix to a 1D array
    features = feature_matrix.flatten()

    # Display the shape of the feature array
    print(f"Shape of the feature array: {features.shape}")

    # Save the feature array to a file if needed
    np.save('mean_pixel_features.npy', features)
    print("Features saved to 'mean_pixel_features.npy'.")
Shape of the feature array: (50400,)
Features saved to 'mean_pixel_features.npy'.

Using TensorFlow/Keras

In [61]:
pip install numpy opencv-python matplotlib tensorflow
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (1.25.2)
Requirement already satisfied: opencv-python in /usr/local/lib/python3.10/dist-packages (4.8.0.76)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (3.7.1)
Requirement already satisfied: tensorflow in /usr/local/lib/python3.10/dist-packages (2.15.0)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.2.1)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (4.53.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (1.4.5)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (24.1)
Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (9.4.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib) (2.8.2)
Requirement already satisfied: absl-py>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.4.0)
Requirement already satisfied: astunparse>=1.6.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.6.3)
Requirement already satisfied: flatbuffers>=23.5.26 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (24.3.25)
Requirement already satisfied: gast!=0.5.0,!=0.5.1,!=0.5.2,>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.5.4)
Requirement already satisfied: google-pasta>=0.1.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: h5py>=2.9.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.9.0)
Requirement already satisfied: libclang>=13.0.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (18.1.1)
Requirement already satisfied: ml-dtypes~=0.2.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.2.0)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.3.0)
Requirement already satisfied: protobuf!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.20.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (3.20.3)
Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from tensorflow) (67.7.2)
Requirement already satisfied: six>=1.12.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.16.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.4.0)
Requirement already satisfied: typing-extensions>=3.6.6 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (4.12.2)
Requirement already satisfied: wrapt<1.15,>=1.11.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.14.1)
Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (0.37.0)
Requirement already satisfied: grpcio<2.0,>=1.24.3 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (1.64.1)
Requirement already satisfied: tensorboard<2.16,>=2.15 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.2)
Requirement already satisfied: tensorflow-estimator<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: keras<2.16,>=2.15.0 in /usr/local/lib/python3.10/dist-packages (from tensorflow) (2.15.0)
Requirement already satisfied: wheel<1.0,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from astunparse>=1.6.0->tensorflow) (0.43.0)
Requirement already satisfied: google-auth<3,>=1.6.3 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.27.0)
Requirement already satisfied: google-auth-oauthlib<2,>=0.5 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (1.2.0)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.6)
Requirement already satisfied: requests<3,>=2.21.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (2.31.0)
Requirement already satisfied: tensorboard-data-server<0.8.0,>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (0.7.2)
Requirement already satisfied: werkzeug>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from tensorboard<2.16,>=2.15->tensorflow) (3.0.3)
Requirement already satisfied: cachetools<6.0,>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (5.3.3)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.4.0)
Requirement already satisfied: rsa<5,>=3.1.4 in /usr/local/lib/python3.10/dist-packages (from google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (4.9)
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.10/dist-packages (from google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (1.3.1)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2.21.0->tensorboard<2.16,>=2.15->tensorflow) (2024.6.2)
Requirement already satisfied: MarkupSafe>=2.1.1 in /usr/local/lib/python3.10/dist-packages (from werkzeug>=1.0.1->tensorboard<2.16,>=2.15->tensorflow) (2.1.5)
Requirement already satisfied: pyasn1<0.7.0,>=0.4.6 in /usr/local/lib/python3.10/dist-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.16,>=2.15->tensorflow) (0.6.0)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.10/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<2,>=0.5->tensorboard<2.16,>=2.15->tensorflow) (3.2.2)
In [65]:
from tensorflow.keras.applications import VGG16
In [66]:
import tensorflow.keras.applications
In [68]:
from tensorflow.keras.applications.vgg16 import preprocess_input
In [69]:
def load_and_preprocess_image(image_path, target_size):
    image = cv2.imread(image_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, target_size)
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
    image = preprocess_input(image)
    return image
In [70]:
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.preprocessing.image import img_to_array

# Load and preprocess the image
image_path = '/content/leaves.jpeg'  # Replace with the path to your image
image = load_and_preprocess_image(image_path, target_size=(224, 224))

# Extract features
base_model = VGG16(weights='imagenet')
model = Model(inputs=base_model.input, outputs=base_model.get_layer('block5_pool').output)
features = model.predict(image)

# Display the shape of the extracted features
print(f"Shape of the extracted features: {features.shape}")

# Flatten the features to create a 1D array
flattened_features = features.flatten()
print(f"Shape of the flattened feature array: {flattened_features.shape}")

# Save the features to a file if needed
np.save('cnn_features.npy', flattened_features)
print("Features saved to 'cnn_features.npy'.")

# Optionally visualize some of the features
def visualize_features(features, num_filters):
    fig, axes = plt.subplots(1, num_filters, figsize=(10, 10))
    for i in range(num_filters):
        axes[i].imshow(features[0, :, :, i], cmap='viridis')
        axes[i].set_title(f"Filter {i}")
        axes[i].axis('off')
    plt.show()

# Visualize the first 8 features
visualize_features(features, 8)
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels.h5
553467096/553467096 [==============================] - 9s 0us/step
1/1 [==============================] - 1s 1s/step
Shape of the extracted features: (1, 7, 7, 512)
Shape of the flattened feature array: (25088,)
Features saved to 'cnn_features.npy'.

Using PyTorch

In [71]:
pip install torch torchvision
Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (2.3.0+cu121)
Requirement already satisfied: torchvision in /usr/local/lib/python3.10/dist-packages (0.18.0+cu121)
Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch) (3.14.0)
Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch) (4.12.2)
Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch) (1.12.1)
Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch) (3.3)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch) (3.1.4)
Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch) (2023.6.0)
Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch)
  Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)
Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch)
  Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)
Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch)
  Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)
Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch)
  Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)
Collecting nvidia-cublas-cu12==12.1.3.1 (from torch)
  Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)
Collecting nvidia-cufft-cu12==11.0.2.54 (from torch)
  Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)
Collecting nvidia-curand-cu12==10.3.2.106 (from torch)
  Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)
Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch)
  Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)
Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch)
  Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)
Collecting nvidia-nccl-cu12==2.20.5 (from torch)
  Using cached nvidia_nccl_cu12-2.20.5-py3-none-manylinux2014_x86_64.whl (176.2 MB)
Collecting nvidia-nvtx-cu12==12.1.105 (from torch)
  Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)
Requirement already satisfied: triton==2.3.0 in /usr/local/lib/python3.10/dist-packages (from torch) (2.3.0)
Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch)
  Downloading nvidia_nvjitlink_cu12-12.5.40-py3-none-manylinux2014_x86_64.whl (21.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.3/21.3 MB 37.5 MB/s eta 0:00:00
Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from torchvision) (1.25.2)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.10/dist-packages (from torchvision) (9.4.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch) (2.1.5)
Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy->torch) (1.3.0)
Installing collected packages: nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, nvidia-cusparse-cu12, nvidia-cudnn-cu12, nvidia-cusolver-cu12
Successfully installed nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.20.5 nvidia-nvjitlink-cu12-12.5.40 nvidia-nvtx-cu12-12.1.105
In [72]:
import torch
import torch.nn as nn
from torchvision import models, transforms
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

# Load a pretrained ResNet model, removing the final classification layer
model = models.resnet50(pretrained=True)
model = nn.Sequential(*list(model.children())[:-1])  # Remove the last classification layer
model.eval()

# Function to display an image
def display_image(image, title):
    plt.imshow(image)
    plt.title(title)
    plt.axis('off')
    plt.show()

# Load and preprocess the image
img_path = '/content/rose.jpeg'  # Replace with the path to your image
image = Image.open(img_path).convert('RGB')
display_image(image, 'Original Image')

preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

image_tensor = preprocess(image).unsqueeze(0)

# Use the ResNet model to extract features
with torch.no_grad():
    features = model(image_tensor)

# Display the shape of the extracted features
print(f"Extracted features shape: {features.shape}")

# Flatten the features to a 1D array
flattened_features = features.view(-1).numpy()
print(f"Flattened features shape: {flattened_features.shape}")

# Save the features to a file if needed
np.save('resnet_features.npy', flattened_features)
print("Features saved to 'resnet_features.npy'.")
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet50_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet50_Weights.DEFAULT` to get the most up-to-date weights.
  warnings.warn(msg)
Downloading: "https://download.pytorch.org/models/resnet50-0676ba61.pth" to /root/.cache/torch/hub/checkpoints/resnet50-0676ba61.pth
100%|██████████| 97.8M/97.8M [00:01<00:00, 64.2MB/s]
Extracted features shape: torch.Size([1, 2048, 1, 1])
Flattened features shape: (2048,)
Features saved to 'resnet_features.npy'.